package com.zhj.edudemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhj.edudemo.bean.Furn;
import com.zhj.edudemo.service.FurnService;
import com.zhj.edudemo.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author zhj
 * @version 1.0
 * description:
 */
@RestController
@RequestMapping("/stu")
@Slf4j
public class FurnController {

    @Resource
    private FurnService furnService;

    @PostMapping("save")
    public Result save(@Validated @RequestBody Furn  furn, Errors errors) { // 增
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        if (map.isEmpty()) {
            log.info("furn={}", furn);
            boolean save = furnService.save(furn);
            return Result.success();
        } else {
            return Result.error("1001", "后端校验新增失败", map);
        }
    }

    @GetMapping("list")
    public Result list() {
        List<Furn> furns = furnService.list();
        Result<List<Furn>> result = Result.success(furns);
        return result;
    }

    @GetMapping("find/{id}")
    public Result getById(@PathVariable Long id) {
        Furn furn = furnService.getById(id);
        if (furn != null) {
            return Result.success(furn);
        } else {
            return Result.error("997", "获取id为" + id + "的家居信息失败！！！");
        }
    }

    @PutMapping("update")
    public Result update(@RequestBody Furn furn) {
//        Furn fullFurn = furnService.getById(furn.getId());
//        furn.setVersion(fullFurn.getVersion());
        boolean b = furnService.updateById(furn);
        if (b) {
            return Result.success();
        } else {
            return Result.error("999", "修改失败!!!");
        }
    }

    @DeleteMapping("del/{id}")
    public Result del(@PathVariable Long id) {
        boolean b = furnService.removeById(id);
        if (b) {
            return Result.success();
        } else {
            return Result.error("998", "删除失败!!!");
        }
    }

    @GetMapping("listByPage")
    public Result listFurnByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<Furn> furnPage = new Page<>(pageNum, pageSize);
        Page<Furn> page = furnService.page(furnPage);
        return Result.success(page);
    }

    @GetMapping("listByConditionPage")
    public Result listFurnByConditionPage(@RequestParam(defaultValue = "") String key,
                                          @RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "5") Integer pageSize) {
        QueryWrapper<Furn> wrapper = new QueryWrapper<>();
        Page<Furn> page = new Page<>(pageNum, pageSize);
        Page<Furn> furnPage = null;
        if (StringUtils.hasText(key)) {
            wrapper.like("name", key);
            furnPage = furnService.page(page, wrapper);
        } else {
            furnPage = furnService.page(page);
        }
        return Result.success(furnPage);
    }

    // 使用lambda query
    @GetMapping("listByConditionPage2")
    public Result listFurnByConditionPage2(@RequestParam(defaultValue = "") String key,
                                          @RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "5") Integer pageSize) {
        LambdaQueryWrapper<Furn> lambdaQuery = Wrappers.<Furn>lambdaQuery();
        Page<Furn> page = new Page<>(pageNum, pageSize);
        Page<Furn> furnPage = null;
        if (StringUtils.hasText(key)) {
            lambdaQuery.like(Furn::getName, key);
            furnPage = furnService.page(page, lambdaQuery);
        } else {
            furnPage = furnService.page(page);
        }
        return Result.success(furnPage);
    }
}
